package cn.ncu.wechat.controller;

import cn.ncu.wechat.dao.OrdersMapper;
import cn.ncu.wechat.model.VerificationRequest;
import cn.ncu.wechat.service.OrdersService;
import cn.ncu.wechat.service.VerificationService;
import cn.ncu.wechat.util.Result;
import cn.ncu.wechat.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
@RequestMapping("/api/admin/verifications")
@RestController
public class VerificationController {
    @Autowired
    private VerificationService verificationService;
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/verify")
    public Result<?> addVerification(@RequestBody VerificationRequest request,@RequestHeader("Authorization") Optional<String> authorizationHeader){
        System.out.println(request);
        if (request.getOrderNo().isBlank()||request.getVerificationCode().isBlank()||request.getStoreId()==0||request.getStoreId()==null){
            return Result.fail(401,"参数错误");
        } else return verificationService.addVerification(request,authorizationHeader.get());
    }

    @GetMapping("")
    public Result<?> getVerifications(@RequestParam  Integer page,@RequestParam Integer pageSize,@RequestParam(required = false) String orderNo,@RequestParam(required = false) String startDate,@RequestParam(required = false) String endDate,@RequestParam(required = false) Integer storeId){
        return ordersService.getVerifications(page,pageSize,
                Optional.ofNullable(orderNo).orElse(""),
                Optional.ofNullable(startDate).orElse(""),
                Optional.ofNullable(endDate).orElse(""),
                storeId);
    }

    @GetMapping("/check")
    public Result<?> checkVerification(@RequestParam(required = true) String verificationCode){
        if (StringUtil.IsNotNull(verificationCode)) return verificationService.checkVerification(verificationCode);
        return Result.fail(401,"核验码不能为空");
    }

    @GetMapping("/statistics")
    public Result<?> sumByDate(@RequestParam String startDate,@RequestParam String endDate,@RequestParam(required = false) Long storeId){
        return verificationService.sumByDate(startDate,endDate,Optional.ofNullable(storeId).orElse(0L));
    }
}
